package com.tablefood.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.tablefood.domain.ShopCategory;
import com.tablefood.domain.ShopUser;
import com.tablefood.service.CategoryService;
import com.tablefood.utils.ResultUtils;


public class CategoryAction extends ActionSupport {

	@Resource
	private CategoryService categoryService;
	
	private List<ShopCategory> listCategory;
	private ShopCategory category;
	private long cateid;
	private String message;
	
 
	public ShopCategory getCategory() {
		return category;
	}
	public void setCategory(ShopCategory category) {
		this.category = category;
	}
	public long getCateid() {
		return cateid;
	}
	public void setCateid(long cateid) {
		this.cateid = cateid;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public List<ShopCategory> getListCategory() {
		return listCategory;
	}
	public void setListCategory(List<ShopCategory> listCategory) {
		this.listCategory = listCategory;
	}
	
	// 显示商品表
	public String categoryList() throws Exception {
		this.listCategory = categoryService.getAllCategory();
		return SUCCESS;
	}

	// 伪删除商品
	public String categoryDel() throws Exception {

		// System.out.println(cateid);
		if (cateid == 0) {
			this.setMessage("删除失败");
			return ERROR;
		}
		
		if(categoryService.hasChild(cateid)){
			this.listCategory = categoryService.getAllCategory();
			this.setMessage("删除失败");
			return SUCCESS;
		}

		if (categoryService.delCategory(cateid)) {
			this.listCategory = categoryService.getAllCategory();
			this.setMessage("删除成功");
			return SUCCESS;
		} else {
			this.setMessage("删除失败");
			return ERROR;
		}
	}

	// 商品详细信息
	public String categoryDetail() throws Exception {

		if (cateid == 0) {
			category = null;
			return SUCCESS;
		}

		this.category = categoryService.getCategoryByCateId(cateid);
		if (this.category != null) {
			return SUCCESS;
		}
		return ERROR;
	}
	
	// 商品信息添加
	public String categoryAdd() throws Exception {

		// 获取post请求的数据
		Map<String, String> param = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String[]> params = request.getParameterMap();
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				param.put(key, values[i]);

			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		if (param.get("title") == "" || param.get("parentid") == "") {
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		// 检索名字有没有重复
		if(categoryService.checkCategoryTitle(param.get("title") )){
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		Long parentid = Long.parseLong(param.get("parentid"));
		if(!categoryService.hasParent(parentid) && parentid != 0){
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		

		// 实例化
		ShopCategory category = new ShopCategory();
		category.setTitle(param.get("title"));
		category.setParentid(parentid);
		category.setState("1");
		
		if(categoryService.addCategory(category)){
			map.put("status", 0);
		}else{
			map.put("status", -1);
		}

		ResultUtils.toJson(ServletActionContext.getResponse(), map);
		return null;
	}
	
	// 商品信息更新
	public String categoryUpadte() throws Exception {
		// 获取post请求的数据
		Map<String, String> param = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String[]> params = request.getParameterMap();
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				param.put(key, values[i]);

			}
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (param.get("id") == "" || param.get("title") == "" || param.get("parentid") == "") {
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		this.cateid = Long.parseLong(param.get("id"));
		ShopCategory category = categoryService.getCategoryByCateId(this.cateid);
		
		
		// 检索名字有没有重复
		if(categoryService.checkCategoryTitle(param.get("title")) && !category.getTitle().equals(param.get("title")) ){
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		// 检查有没有父亲
		Long parentid = Long.parseLong(param.get("parentid"));
		//System.out.println(!categoryService.hasParent(parentid));
		//System.out.println(categoryService.hasChild(this.cateid));
		if(category.getParentid() != parentid && !categoryService.hasParent(parentid) || categoryService.hasChild(this.cateid)){
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		// 实例化
		category.setTitle(param.get("title"));
		category.setParentid(parentid);
		category.setState("1");
		
		if(categoryService.updateCategory(category)){
			map.put("status", 0);
		}else{
			map.put("status", -1);
		}

		ResultUtils.toJson(ServletActionContext.getResponse(), map);
		return null;
	}
	
}
